package com.example.blog.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.blog.entity.Comment;
import com.example.blog.entity.Reply;
import com.example.blog.service.CommentService;

/**
 * 评论控制器
 */
@RestController
@RequestMapping("/api/comment")
@CrossOrigin
public class CommentController {

    @Autowired
    private CommentService commentService;
    
    /**
     * 发表评论
     */
    @PostMapping("/add")
    public Map<String, Object> addComment(@RequestBody Comment comment) {
        return commentService.addComment(comment);
    }
    
    /**
     * 回复评论
     */
    @PostMapping("/reply")
    public Map<String, Object> addReply(@RequestBody Reply reply) {
        return commentService.addReply(reply);
    }
    
    /**
     * 更新评论
     */
    @PutMapping("/update")
    public Map<String, Object> updateComment(@RequestBody Comment comment) {
        Map<String, Object> map = new HashMap<>();
        boolean result = commentService.updateComment(comment);
        if (result) {
            map.put("success", true);
            map.put("message", "更新成功");
        } else {
            map.put("success", false);
            map.put("message", "更新失败");
        }
        return map;
    }
    
    /**
     * 更新回复
     */
    @PutMapping("/reply/update")
    public Map<String, Object> updateReply(@RequestBody Reply reply) {
        Map<String, Object> map = new HashMap<>();
        boolean result = commentService.updateReply(reply);
        if (result) {
            map.put("success", true);
            map.put("message", "更新成功");
        } else {
            map.put("success", false);
            map.put("message", "更新失败");
        }
        return map;
    }
    
    /**
     * 删除评论
     */
    @DeleteMapping("/{commentId}")
    public Map<String, Object> deleteComment(@PathVariable Integer commentId) {
        Map<String, Object> map = new HashMap<>();
        boolean result = commentService.deleteComment(commentId);
        if (result) {
            map.put("success", true);
            map.put("message", "删除成功");
        } else {
            map.put("success", false);
            map.put("message", "删除失败");
        }
        return map;
    }
    
    /**
     * 删除回复
     */
    @DeleteMapping("/reply")
    public Map<String, Object> deleteReply(@RequestParam Integer userId, @RequestParam Integer commentId) {
        Map<String, Object> map = new HashMap<>();
        boolean result = commentService.deleteReply(userId, commentId);
        if (result) {
            map.put("success", true);
            map.put("message", "删除成功");
        } else {
            map.put("success", false);
            map.put("message", "删除失败");
        }
        return map;
    }
    
    /**
     * 标记评论为用户删除
     */
    @PutMapping("/mark-deleted/{commentId}")
    public Map<String, Object> markCommentAsDeletedByUser(@PathVariable Integer commentId) {
        Map<String, Object> map = new HashMap<>();
        boolean result = commentService.markCommentAsDeletedByUser(commentId);
        if (result) {
            map.put("success", true);
            map.put("message", "标记删除成功");
        } else {
            map.put("success", false);
            map.put("message", "标记删除失败");
        }
        return map;
    }
    
    /**
     * 标记回复为用户删除
     */
    @PutMapping("/reply/mark-deleted")
    public Map<String, Object> markReplyAsDeletedByUser(@RequestParam Integer userId, @RequestParam Integer commentId) {
        Map<String, Object> map = new HashMap<>();
        boolean result = commentService.markReplyAsDeletedByUser(userId, commentId);
        if (result) {
            map.put("success", true);
            map.put("message", "标记删除成功");
        } else {
            map.put("success", false);
            map.put("message", "标记删除失败");
        }
        return map;
    }
    
    /**
     * 获取评论详情
     */
    @GetMapping("/{commentId}")
    public Map<String, Object> getCommentById(@PathVariable Integer commentId) {
        Map<String, Object> map = new HashMap<>();
        Comment comment = commentService.getCommentById(commentId);
        if (comment != null) {
            map.put("success", true);
            map.put("comment", comment);
        } else {
            map.put("success", false);
            map.put("message", "评论不存在");
        }
        return map;
    }
    
    /**
     * 获取博客的所有评论
     */
    @GetMapping("/blog/{blogId}")
    public List<Comment> getCommentsByBlogId(@PathVariable Integer blogId) {
        return commentService.getCommentsByBlogId(blogId);
    }
    
    /**
     * 获取博客的有效评论
     */
    @GetMapping("/blog/{blogId}/valid")
    public List<Comment> getValidCommentsByBlogId(@PathVariable Integer blogId) {
        return commentService.getValidCommentsByBlogId(blogId);
    }
    
    /**
     * 获取评论的所有回复
     */
    @GetMapping("/{commentId}/replies")
    public List<Reply> getRepliesByCommentId(@PathVariable Integer commentId) {
        return commentService.getRepliesByCommentId(commentId);
    }
    
    /**
     * 获取评论的有效回复
     */
    @GetMapping("/{commentId}/replies/valid")
    public List<Reply> getValidRepliesByCommentId(@PathVariable Integer commentId) {
        return commentService.getValidRepliesByCommentId(commentId);
    }
    
    /**
     * 获取用户的所有评论
     */
    @GetMapping("/user/{userId}")
    public List<Comment> getCommentsByUserId(@PathVariable Integer userId) {
        return commentService.getCommentsByUserId(userId);
    }
    
    /**
     * 获取用户的所有回复
     */
    @GetMapping("/reply/user/{userId}")
    public List<Reply> getRepliesByUserId(@PathVariable Integer userId) {
        return commentService.getRepliesByUserId(userId);
    }
    
    /**
     * 管理员禁用/启用评论
     */
    @PutMapping("/admin/toggle-status/{commentId}")
    public Map<String, Object> toggleCommentStatus(
            @PathVariable Integer commentId,
            @RequestParam Boolean isDisabled) {
        Map<String, Object> map = new HashMap<>();
        boolean result = commentService.updateCommentStatus(commentId, isDisabled);
        if (result) {
            map.put("success", true);
            map.put("message", isDisabled ? "评论已禁用" : "评论已启用");
        } else {
            map.put("success", false);
            map.put("message", "操作失败");
        }
        return map;
    }
    
    /**
     * 管理员禁用/启用回复
     */
    @PutMapping("/admin/reply/toggle-status")
    public Map<String, Object> toggleReplyStatus(
            @RequestParam Integer userId,
            @RequestParam Integer commentId,
            @RequestParam Boolean isDisabled) {
        Map<String, Object> map = new HashMap<>();
        boolean result = commentService.updateReplyStatus(userId, commentId, isDisabled);
        if (result) {
            map.put("success", true);
            map.put("message", isDisabled ? "回复已禁用" : "回复已启用");
        } else {
            map.put("success", false);
            map.put("message", "操作失败");
        }
        return map;
    }
}